<template>
  <div class="box">
    <el-tabs v-model="activeName">
      <el-tab-pane label="角色管理" name="first">
        <el-button type="primary" size="small" @click="addDialogVisible=true;title='新增角色'">新增角色</el-button>
        <el-table v-model="loading" :data="roleList" border stripe style="margin-top:30px;">
          <!-- <el-table-column type="index" label="序号" align="center" width="100px" /> -->
          <!-- 修改每页序号按顺序排列s -->
          <el-table-column type="index" label="序号" align="center" width="100px">
            <template #default="{$index}">
              {{ (params.page-1)*params.pagesize+$index+1 }}
            </template>
          </el-table-column>
          <!-- 修改每页序号按顺序排列e -->
          <el-table-column prop="description" label="角色" align="center" />
          <el-table-column prop="name" label="描述" align="center" />
          <el-table-column label="操作" align="center">
            <template #default="{row}">
              <el-button icon="el-icon-setting" type="success" size="small" @click="showPermission(row.id)">分配权限</el-button>
              <el-button icon="el-icon-edit" type="primary" size="small" @click="handleEdit(row)">编辑</el-button>
              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <el-popconfirm
                title="这是一段内容确定删除吗？"
                @onConfirm="handelDelate(row.id)"
              >
                <el-button slot="reference" icon="el-icon-delete" type="danger" size="small">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <div style="text-align:right;margin-top:20px">
          <el-pagination
            :page-size="params.pagesize"
            :total="total"
            layout="prev, pager, next"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-tab-pane>
      <el-tab-pane label="公司信息" name="second">
        <el-alert
          title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
          type="info"
          show-icon
          :closable="false"
        />
        <el-form label-width="120px" style="width:500px;margin-top:40px">
          <el-form-item label="公司名称">
            <el-input v-model="companyInfo.name" disabled />
          </el-form-item>
          <el-form-item label="公司地址">
            <el-input v-model="companyInfo.companyAddress" disabled />
          </el-form-item>
          <el-form-item label="公司邮箱">
            <el-input v-model="companyInfo.mailbox" disabled />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="companyInfo.remarks" disabled type="textarea" />
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>
    <!-- 如果浮出来的dialog浮出来的页面就写在最外层的div上面 -->
    <!-- 新增角色页面S -->
    <el-dialog :title="title" :visible.sync="addDialogVisible" width="50%" @close="handleChange">
      <el-form ref="form" label-width="120px" :model="form" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="form.description" />
        </el-form-item>
      </el-form>
      <div slot="footer" style="text-align:center;">
        <el-button size="small" @click="addDialogVisible=false">取消</el-button>
        <el-button type="primary" size="small" @click="add">确认</el-button>
      </div>
    </el-dialog>
    <!-- 新增角色页面E-->
    <AssignPermission :id="id" :show.sync="showDialog" />
  </div>
</template>

<script>
import { getCompanyInfo, getRoleList, addRole, deleteRole, updateRole } from '@/api/setting'
import { createNamespacedHelpers } from 'vuex'
const { mapState } = createNamespacedHelpers('user')
import AssignPermission from './components/assign-permission'
export default {
  name: 'SettingPage',
  components: {
    AssignPermission
  },
  data() {
    return {
      activeName: 'first',
      companyInfo: {}, // 如果拿回来的数据是对象初始值是{}，数组是[],基本类型null
      params: {
        page: 1,
        pagesize: 8
      },
      total: null, // 数据总条数
      roleList: [],
      loading: false, // 缓存
      addDialogVisible: false,
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '不能为空', trigger: 'blur' }]

      },
      title: '',
      showDialog: false, // 分配权限
      id: null
    }
  },
  computed: {
    ...mapState(['userInfo'])
  },
  watch: {},
  created() {
    this.getCompanyInfo()
    this.getRoleList(this.params)
  },
  methods: {
    // 获取公司信息
    async getCompanyInfo() {
      // if (!this.userInfo.companyId) {
      //   await this.$store.dispatch('user/getUserInfo')
      // }
      this.companyInfo = await getCompanyInfo(this.userInfo.companyId)
      console.log(this.companyInfo)
    },
    // 渲染主页的axios请求数据，获取角色列表
    async getRoleList() {
      this.loading = true
      const { total, rows } = await getRoleList(this.params)
      this.total = total
      this.roleList = rows
      this.loading = false
    },
    // 点击切换页数
    handleCurrentChange(current) {
      console.log(current)
      this.params.page = current
      this.getRoleList()
    },
    handleChange() {
      // resetFields除了表单重置，还有那个校验规则重置
      this.$refs.form.resetFields()
      // 这一步，主要是编辑时候退出再打开新增，表格里数据清空
      this.form = {}
    },
    // 新增功能 确定，需要预校验
    async add() {
      try {
        await this.$refs.form.validate()
        // 区分新增编辑，发请求,拿到的数据不一致
        if (this.title === '新增角色') {
          await addRole(this.form)
        } else {
          await updateRole(this.form)
        }
        this.getRoleList() // 重新渲染页面
        this.addDialogVisible = false
      } catch (err) {
        console.log(err)
      }
    },
    // 删除功能
    async handelDelate(id) {
      await deleteRole(id)
      this.$message.success('删除成功')
      // 当当前页数据只剩最后一条且当前页不是第一页时，删除最后一条数据就直接拿上一页数据
      if (this.roleList.length === 1 && this.params.page !== 1) {
        this.params.page--
      }
      this.getRoleList()// 删除现在的数据重新渲染页面
    },
    // 编辑事件
    handleEdit(row) {
      this.title = '编辑角色'
      this.form = { ...row } // 解决编辑表格数据和表单数据同步更新问题，这一步把数据区分两份，地址不一样，不会互相影响
      this.addDialogVisible = true // 点击编辑按钮打开dialog表单
    },
    showPermission(id) {
      this.id = id
      this.showDialog = true
    }

  }

}
</script>

<style scoped lang='scss'>
.box{
  padding: 20px;
}
</style>
